package com.ruoyi.order.enums;

import com.ruoyi.common.exception.ServiceException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 订单状态:1:未付款；2：已付款；3：已完工;4:取消订单;5:超时取消订单;6:进行中
 */
public enum OrderStatusEnum {

    WAIT_PAY(1, "待付款"),
    PAID(2, "已付款"), //未开始
    FINISH(3, "已完工"), //已结束
    CANCEL_ORDER(4, "取消订单"),
    TIMEOUT_CANCEL_ORDER(5, "超时取消订单"),
    UNDER_WAY(6, "进行中"),

    ;
    private Integer code;
    private String description;

    private static List<OrderStatusEnum> statusEnumList = new ArrayList<>();

    static {
        statusEnumList.add(WAIT_PAY);
        statusEnumList.add(PAID);
        statusEnumList.add(FINISH);
        statusEnumList.add(CANCEL_ORDER);
        statusEnumList.add(TIMEOUT_CANCEL_ORDER);
        statusEnumList.add(UNDER_WAY);
    }

    OrderStatusEnum(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    public Integer getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public static OrderStatusEnum getOrderStatusEnumByCode(Integer code) {
        switch (code) {
            case 1:
                return WAIT_PAY;
            case 2:
                return PAID;
            case 3:
                return FINISH;
            case 4:
                return CANCEL_ORDER;
            case 5:
                return TIMEOUT_CANCEL_ORDER;
            case 6:
                return UNDER_WAY;
            default:
                throw new ServiceException("错误订单状态类型");
        }
    }

    public static String getOrderStatusEnumByCodeToApp(Integer code) {
        switch (code) {
            case 1:
                return WAIT_PAY.getDescription();
            case 2:
            case 6:
            case 3:
                return PAID.getDescription();
            case 4:
                return CANCEL_ORDER.getDescription();
            case 5:
                return TIMEOUT_CANCEL_ORDER.getDescription();
            default:
                throw new ServiceException("错误订单状态类型");
        }
    }

    /**
     * 是否合法
     *
     * @param code
     * @return
     */
    public static Boolean isExist(Integer code) {
        OrderStatusEnum statusEnum = getOrderStatusEnumByCode(code);
        return statusEnumList.contains(statusEnum);
    }

    /**
     * @throws
     * @MethodName getOrderStatusEnum
     * @author zhouzihao
     * @param: orderStatusAppEnum
     * @DateTime 2024年11月8日, 0008 下午 05:22
     * @return: java.util.List<java.lang.Integer>
     * @description:前台转后端参数
     */
    public static List<Integer> getOrderStatusEnum(OrderStatusAppEnum orderStatusAppEnum) {
        switch (orderStatusAppEnum) {
            //待付款
            case WAIT_PAY:
                return Arrays.asList(OrderStatusEnum.WAIT_PAY.getCode());
            //已报名
            case PAID:
                return Arrays.asList(OrderStatusEnum.PAID.getCode(), OrderStatusEnum.UNDER_WAY.getCode());
            //已完工
            case FINISH:
                return Arrays.asList(OrderStatusEnum.FINISH.getCode());
            //取消订单
            case CANCEL_ORDER:
                return Arrays.asList(OrderStatusEnum.CANCEL_ORDER.getCode(), OrderStatusEnum.TIMEOUT_CANCEL_ORDER.getCode());
            default:
                throw new ServiceException("错误订单状态类型");
        }
    }
}
